package nac.firefight;

//~--- non-JDK imports --------------------------------------------------------
import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.loading.DeferredResource;
import org.newdawn.slick.loading.LoadingList;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.Log;

//~--- JDK imports ------------------------------------------------------------

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import org.newdawn.slick.tiled.TiledMap;

/**
 *
 *
 * @author nac
 */
public class LoadingState extends AbstractGameState {

    private Image backGround;
    private Image logo;
    private DeferredResource nextResource;
    private boolean started;

    public LoadingState(int id, FireFightGame ffg) {
        super(id, ffg);
    }

    private void loadImage(String res) throws SlickException {
        File file = new File(res + "/image", ".");
        String[] imageNames = file.list(new SVNFilter());

        for (int i = 0; i < imageNames.length; i++) {
            if (!("Thumbs.db").equals(imageNames[i])) {
                game.getResourceMap().addResource(imageNames[i], new Image(res + "/image/" + imageNames[i]));
                Log.info("Loading:" + imageNames[i]);
            }
        }
    }

    private void loadSound(String res) throws SlickException {
        File file = new File(res + "/sound", ".");
        String[] soundNames = file.list(new SVNFilter());

        for (int i = 0; i < soundNames.length; i++) {
            game.getResourceMap().addResource(soundNames[i], new Sound(res + "/sound/" + soundNames[i]));
            Log.info("Loading:" + soundNames[i]);
        }
    }

    private void loadMusic(String res) throws SlickException {
        File file = new File(res + "/music", ".");
        String[] musicNames = file.list(new SVNFilter());

        for (int i = 0; i < musicNames.length; i++) {
            game.getResourceMap().addResource(musicNames[i], new Music(res + "/music/" + musicNames[i]));
            Log.info("Loading:" + musicNames[i]);
        }
    }

    private void loadAngelCodeFont(String res) throws SlickException {
        File file = new File(res + "/angelcodefont/font", ".");
        String[] fontNames = file.list(new SVNFilter());

        file = new File(res + "/angelcodefont/image", ".");
        String[] fontImg = file.list(new SVNFilter());

        for (int i = 0; i < fontNames.length; i++) {
            for (int j = 0; j < fontImg.length; j++) {
                String str1 = fontNames[i].replaceAll(".fnt", "");
                String str2 = fontImg[j].replaceAll(".tga", "");

                if (str1.equals(str2)) {
                    game.getResourceMap().addResource(fontNames[i],
                            new AngelCodeFont(res + "/angelcodefont/font/" + fontNames[i],
                            res + "/angelcodefont/image/" + fontImg[j]));
                    Log.info("Loading: " + fontNames[i]);
                }
            }
        }
    }

    private void loadTiledMap(String res) throws SlickException {
        File file = new File(res + "/tiledmap", ".");
        String[] mapNames = file.list(new SVNFilter());
        for (int i = 0; i < mapNames.length; i++) {
            if (mapNames[i].substring(mapNames[i].length() - 3, mapNames[i].length()).equals("tmx")) {
                game.getResourceMap().addResource(mapNames[i], new TiledMap(res + "/tiledmap/" + mapNames[i]));
                Log.info("Loading:" + mapNames[i]);
            }

        }
    }

    public void init(GameContainer container, StateBasedGame sb) throws SlickException {
        backGround = new Image("sky.png");
        logo = new Image("logo.png");
        LoadingList.setDeferredLoading(true);

        String res = FireFightGame.RESOURCE_DIR;

        loadImage(res);
        loadSound(res);
        loadMusic(res);
        loadAngelCodeFont(res);
        loadTiledMap(res);
    }

    public void render(GameContainer container, StateBasedGame sb, Graphics g) {
        backGround.draw(0, 0);

        int total = LoadingList.get().getTotalResources();
        int loaded = LoadingList.get().getTotalResources() - LoadingList.get().getRemainingResources();

        g.setColor(Color.lightGray);
        g.fillRect(100, 550, loaded * 20, 4);
        g.drawRect(100, 550, total * 20, 4);
        logo.draw((50 * loaded) / 3, 50);

        if (started) {
        }

        if (nextResource != null) {
            g.setColor(Color.white);
            g.drawString("Loading..", 100, 500);
        }
    }

    public void update(GameContainer container, StateBasedGame sb, int delta) throws SlickException {
        if (nextResource != null) {
            try {
                nextResource.load();

                // slow down loading for example purposes
            } catch (IOException e) {
                throw new SlickException("Failed to load: " + nextResource.getDescription(), e);
            }

            nextResource = null;
        }

        if (LoadingList.get().getRemainingResources() > 0) {
            nextResource = LoadingList.get().getNext();
        } else {
            if (!started) {
                started = true;
            }
        }
    }

    @Override
    public void keyPressed(int key, char c) {
        if (key == Input.KEY_ENTER) {
            if (started) {
                game.enterState(FireFightGame.GAMEPLAY);
            }
        }
    }

    private class SVNFilter implements FilenameFilter {

        @Override
        public boolean accept(File dir, String name) {
            boolean accept = true;
            if (name.startsWith(".")) {
                accept = false;
            }
            return accept;
        }
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
